package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachplanApi;
import com.xuecheng.api.content.model.TeachplanDTO;
import com.xuecheng.api.content.model.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.api.media.model.vo.BindTeachplanMediaVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程计划控制层
 * </p>
 */
@RestController
public class TeachplanController implements TeachplanApi {

    @Autowired
    private TeachplanService teachplanService;

    //查询课程中的所有子章节
    @GetMapping("teachplan/{courseId}/tree-nodes")
    public TeachplanDTO queryTreeNodesByCourseId(@PathVariable Long courseId) {
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanDTO dto = teachplanService.queryTreeNodesByCourseId(courseId, companyId);
        return dto;
    }
    //删除课程中的子章节
    @DeleteMapping("teachplan/{teachplanId}")
    public void removeTeachPlan(@PathVariable Long teachplanId) {

        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();

        // 2.调用service方法
        teachplanService.removeTeachPlan(teachplanId,companyId);
    }

    //添加或者修改课程的子章节
    @PostMapping("teachplan")
    public TeachplanDTO createOrModifyTeachPlan(@RequestBody TeachplanVO teachplanVO) {

        //1. 获得公司Id
        Long companyId = SecurityUtil.getCompanyId();

        //2. 将vo数据转为 dto 数据
        TeachplanDTO dto = TeachplanConvert.INSTANCE.vo2dto(teachplanVO);

        //2.调用service获得 dto数据
        TeachplanDTO result = teachplanService.createOrModifyTeachPlan(dto, companyId);

        return result;

    }
    //课程绑定媒资信息
    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {

        Long companyId = SecurityUtil.getCompanyId();

        TeachplanMediaDTO teachplanMediaDTO = new TeachplanMediaDTO();

        teachplanMediaDTO.setTeachplanId(vo.getTeachplanId());
        teachplanMediaDTO.setMediaId(vo.getMediaId());

        TeachplanMediaDTO resultDTO = teachplanService.associateMedia(teachplanMediaDTO, companyId);
        return resultDTO;
    }
}